var util = require('../../utils/util')
var api = require('../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    members: [],
    role: '',


    cardCur: 0,
    swiperList: [
      {
        id: 0,
        type: 'image',
        url: 'https://pipelinenew.oss-cn-chengdu.aliyuncs.com/pic/202412101.jpg'
      },
      {
        id: 1,
        type: 'image',
        url: 'https://pipelinenew.oss-cn-chengdu.aliyuncs.com/pic/202412102.jpg',
      }, {
        id: 2,
        type: 'image',
        url: 'https://pipelinenew.oss-cn-chengdu.aliyuncs.com/pic/202412103.jpg'
      }
      , {
        id: 3,
        type: 'image',
        url: 'https://pipelinenew.oss-cn-chengdu.aliyuncs.com/pic/202412104.jpg'
      }
      //   , {
      //     id: 4,
      //     type: 'image',
      //     url: 'https://hiddengwb.oss-cn-chengdu.aliyuncs.com/pic/20231102163844.jpg'
      //   }
    ],

    TabCur: 0,
    scrollLeft: 0,
    newtab: [
      { id: 0, name: '奖励公告' }, { id: 1, name: '科普园地' }
    ],
    infoList: []
  },


  /**,{id:2,name:'互动交流'}
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {


    this.towerSwiper('swiperList');
    // 初始化towerSwiper 传已有的数组名即可
    //获取新闻列表
    //this.listInfo(0);

    //获取奖励
    //this.getrewards();

    //判断是否填写
    this.checkTime();

  },


  checkTime() {
    setTimeout(this.checkLoginStatus, 3000)

  },


  checkLoginStatus() {

    const userInfoDto = wx.getStorageSync('userInfoDto')

    if (userInfoDto.phone === null || userInfoDto.phone === '') {

      //弹出引导框
      wx.showModal({
        title: '未注册信息',
        content: '请完善个人信息,以便更好的为您服务！',
        success: res => {
          if (res.confirm) {
            wx.navigateTo({
              url: '../home/userinfo'
            })
          } else if (res.cancel) {
            wx.exitMiniProgram({
              success: function () {
                //推出小程序
              }
            })
          }
        }
      })

    }
  },

  checkEmpty(obj) {
    if (typeof obj == "undefined" || obj == null || obj == "") {
      return true;
    } else {
      return false;
    }
  },

  getrewards() {
    let that = this
    util.request(api.getTopReward, 'GET').then(function (res) {

      if (res.code == 200) {

        that.setData({
          members: res.data
        })

      } else {
        util.showErrorToast(res.message);
      }

    }).catch((err) => {
      console.log("登录远程服务器catch:" + err);
      //reject(err);
    });

  },

  checkPhone() {
    wx.makePhoneCall({
      phoneNumber: '13388101468',
      success: function (res) {
        console.log(res)
      },
      fail: function (err) {
        console.log(err)
      }
    })
  },
  reportMessge() {
    wx.navigateTo({
      url: '/pages/report/report'
    })
  },

  question() {
    wx.navigateTo({
      url: '/pages/anwser/queestlist'
    })
  },

  shop() {
    wx.navigateTo({
      url: '/pages/produce/producelist'
    })
  },

  tasklist() {
    wx.navigateTo({
      url: '/pages/task/tasklist'
    })
  },
  myinfo() {
    wx.navigateTo({
      url: '/pages/home/home'
    })
  },
  verify() {
    wx.navigateTo({
      url: '/pages/verify/verifylist'
    })
  },

  up() {
    wx.navigateTo({
      url: '/pages/reportmanage/reportmanagelist'
    })
  },
  monitoring() {
    wx.navigateTo({
      url: '/pages/monitoring/homeindex/homeindex'
    })
  },

  audit() {
    wx.navigateTo({
      url: '/pages/audit/auditlist'
    })
  },

  //获取新闻列表
  listInfo(type) {
    var userinfo = wx.getStorageSync('userInfoDto');
    let that = this
    let infoReq = {}
    infoReq.pageIndex = 1;
    infoReq.pageSize = 499;
    infoReq.type = type;
    infoReq.userId = userinfo.userId
    console.log(JSON.stringify(infoReq))
    util.request(api.listInfo, JSON.stringify(infoReq), 'POST').then(function (res) {

      if (res.code == 200) {

        that.setData({
          infoList: res.data.rows
        })

      } else {
        util.showErrorToast(res.message);
      }

    }).catch((err) => {
      console.log("登录远程服务器catch:" + err);
      //reject(err);
    });

  },

  //跳转详情
  jumpDetail(event) {
    let row = event.currentTarget.dataset.data;

    let infotype = event.currentTarget.dataset.data.type;
    if (infotype === 2) {
      wx.navigateTo({
        url: '/pages/question/questionview?infoId=' + row.infoId
      })
    } else {
      wx.navigateTo({
        url: '/pages/notice/notice?infoId=' + row.infoId
      })
    }
  },




  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.checkMeunTime()

  },

  checkMeunTime() {
    setTimeout(this.initMeun, 3000)

  },

  initMeun() {
    const userInfoDto1 = wx.getStorageSync('userInfoDto')
    if(Object.keys(userInfoDto1).length <= 0) {
      wx.showModal({
        title: '登录失败',
        content: '请退出应用后，重新进入！',
        confirmText: '确定',
        success: res => {
          wx.exitMiniProgram({
            success: function () {
              //推出小程序
            }
          })
        }
    })
      return false
    }

    if (userInfoDto1.roleCodeList.length === 0) {
      //游客角色
      this.setData({
        role: ''
      })
    } else if (userInfoDto1.roleCodeList.includes('role_audit')) {
      //审核管理员
      this.setData({
        role: '2'
      })
    } else if (userInfoDto1.roleCodeList.includes('role_verify')) {
      //信息核实
      this.setData({
        role: '1'
      })
    }else if (userInfoDto1.roleCodeList.includes('role_supervise') || userInfoDto1.roleCodeList.includes('role_biz') || userInfoDto1.roleCodeList.includes('role_safety') || userInfoDto1.roleCodeList.includes('role_charge_leader') || userInfoDto1.roleCodeList.includes('role_main_leader')) {
      //审核管理
      this.setData({
        role: '3'
      })
    }

  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  tabSelect(e) {

    this.setData({
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id - 1) * 60
    })
    //切换查询数据
    this.listInfo(e.currentTarget.dataset.id);
  },




  DotStyle(e) {
    this.setData({
      DotStyle: e.detail.value
    })
  },
  // cardSwiper
  cardSwiper(e) {
    this.setData({
      cardCur: e.detail.current
    })
  },
  // towerSwiper
  // 初始化towerSwiper
  towerSwiper(name) {
    let list = this.data[name];
    for (let i = 0; i < list.length; i++) {
      list[i].zIndex = parseInt(list.length / 2) + 1 - Math.abs(i - parseInt(list.length / 2))
      list[i].mLeft = i - parseInt(list.length / 2)
    }
    this.setData({
      swiperList: list
    })
  },
  // towerSwiper触摸开始
  towerStart(e) {
    this.setData({
      towerStart: e.touches[0].pageX
    })
  },
  // towerSwiper计算方向
  towerMove(e) {
    this.setData({
      direction: e.touches[0].pageX - this.data.towerStart > 0 ? 'right' : 'left'
    })
  },
  // towerSwiper计算滚动
  towerEnd(e) {
    let direction = this.data.direction;
    let list = this.data.swiperList;
    if (direction == 'right') {
      let mLeft = list[0].mLeft;
      let zIndex = list[0].zIndex;
      for (let i = 1; i < list.length; i++) {
        list[i - 1].mLeft = list[i].mLeft
        list[i - 1].zIndex = list[i].zIndex
      }
      list[list.length - 1].mLeft = mLeft;
      list[list.length - 1].zIndex = zIndex;
      this.setData({
        swiperList: list
      })
    } else {
      let mLeft = list[list.length - 1].mLeft;
      let zIndex = list[list.length - 1].zIndex;
      for (let i = list.length - 1; i > 0; i--) {
        list[i].mLeft = list[i - 1].mLeft
        list[i].zIndex = list[i - 1].zIndex
      }
      list[0].mLeft = mLeft;
      list[0].zIndex = zIndex;
      this.setData({
        swiperList: list
      })
    }
  }


})